<?php

namespace App\Http\Controllers\MapOfFood;

use App\Http\Controllers\Controller;
use App\Models\Bus_info;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;

class MapOfFoodController extends Controller
{
	/**
     * 获取商家信息
     * @param $id
     * @return json
     */
    public function getBusInfo($id){
    	$bus_info = DB::table('bus_infos')->where('id',$id)->get();
    	return $bus_info;
    }
    /**
     * 获取商家信息列表
     * @param $curPage
     * @return array
     */
    public function getBusInfoList($curPage){
    	$start = 10*($curPage-1);
    	$busInfoList = DB::table('bus_infos')->offset($start)->limit(10)->get();
    	return $busInfoList;

    }
    /**
     * 后台获取商家信息列表
     * @param $curPage
     * @return array
     */
    public function getBusInfoListAdmin(){
    	// $busInfoList = DB::table('bus_info')->paginate(10);
    	$busInfoList = DB::table('bus_infos')->where('isDelete','<>','1')->orderBy('create_time','desc')->get();
    	return view('admin/index')->with('busInfoList',$busInfoList);

    }
    /**
     * 添加商家信息
     * @param Request $request
     * @return json
     */
    public function addBusInfo(Request $request){
    	$res = ['status' => 0,'msg' => '添加失败'];
    	//图片上传处理
    	$imgUrl="";
    	if ($request->hasFile('imgUrl')) {
	        $picture = $request->file('imgUrl');
	        if (!$picture->isValid()) {
	            $res = ['status' => 0,'msg' => '无效的上传文件'];
	            return json_encode($res);
	        }
	        // 文件扩展名
	        $extension = $picture->getClientOriginalExtension();
	        // 文件名
	        $fileName = $picture->getClientOriginalName();
	        // 生成新的统一格式的文件名
	        $newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension;
	        // 图片保存路径
	        $savePath = 'images/BusInfoImgs/' . $newFileName;
	        if ($picture->storePubliclyAs('images/BusInfoImgs', $newFileName, ['disk' => 'local'])) {
	            $imgUrl = 'uploads/'.$savePath;
	        }
	    } else {
	        $res = ['status' => 0,'msg' => '图片不能为空'];
	        return json_encode($res);
	    }
    	
    	//插入数据
    	$id = DB::table('bus_infos')->insertGetId([
    		'name' => $request->input('name'),
    		'address' => $request->input('address'),
    		'tel' => $request->input('tel'),
    		'stars' => $request->input('stars'),
    		'isHot' => $request->input('isHot')?$request->input('isHot'):0,
    		'imgUrl' => $imgUrl,
    		'longitude' => $request->input('longitude'),
    		'latitude' => $request->input('latitude')
    	]);
    	

    	if($id >= 1){
    		$res = ['status' => 1,'msg' => '添加成功'];
    	}
    	
    	return json_encode($res);

    }
    /**
     * 更新商家信息
     * @param $id,Request $request
     * @return json
     */
    public function updateBusInfo(Request $request,$id){
    	$res = ['status' => 0,'msg' => '更新失败'];
    	//图片上传处理
    	$imgUrl="";
    	if ($request->hasFile('imgUrl')) {
	        $picture = $request->file('imgUrl');
	        if (!$picture->isValid()) {
	            $res = ['status' => 0,'msg' => '无效的上传文件'];
	            return json_encode($res);
	        }
	        // 文件扩展名
	        $extension = $picture->getClientOriginalExtension();
	        // 文件名
	        $fileName = $picture->getClientOriginalName();
	        // 生成新的统一格式的文件名
	        $newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension;
	        // 图片保存路径
	        $savePath = 'images/BusInfoImgs/' . $newFileName;
	        if ($picture->storePubliclyAs('images/BusInfoImgs', $newFileName, ['disk' => 'local'])) {
	            $imgUrl = 'uploads/'.$savePath;
	        }
	    } else {
	        $id = DB::table('bus_infos')->where('id',$id)->update([
	        	'name' => $request->input('name'),
	        	'address' => $request->input('address'),
	        	'tel' => $request->input('tel'),
	        	'stars' => $request->input('stars'),
	        	'isHot' => $request->input('isHot')?$request->input('isHot'):0,
	        	'longitude' => $request->input('longitude'),
	        	'latitude' => $request->input('latitude')
	        ]);
	        if($id >= 1){
	        	$res = ['status' => 1,'msg' => '更新成功'];
	        }
	        
	        return json_encode($res);
	    }

    	$id = DB::table('bus_infos')->where('id',$id)->update([
    		'name' => $request->input('name'),
    		'address' => $request->input('address'),
    		'tel' => $request->input('tel'),
    		'stars' => $request->input('stars'),
    		'isHot' => $request->input('isHot')?$request->input('isHot'):0,
    		'imgUrl' => $imgUrl,
    		'longitude' => $request->input('longitude'),
    		'latitude' => $request->input('latitude')
    	]);
    	if($id >= 1){
    		$res = ['status' => 1,'msg' => '更新成功'];
    	}
    	
    	return json_encode($res);
    }

    /**
     * 删除商家信息
     * @param $id
     * @return json
     */
    public function deleteBusInfo($id){
    	$bus_info = DB::table('bus_infos')->where('id',$id)->update(['isDelete' => 1]);

    	$res = ['status' => 0,'msg' => '删除失败'];
    	if($bus_info >= 1){
    		$res = ['status' => 1,'msg' => '删除成功'];
    	}
    	
    	return json_encode($res);
    }


    /**
     * 查询附近商家信息
     * @param Request $request
     * @return json
     */
    public function getNearbyBusInfoList(Request $request){
    	$curLongitude = $request->input('curLongitude');
    	$curLatitude = $request->input('curLatitude');
    	$radius = $request->input('radius');
    	//计算正切正方形边界
    	$er = 6371;//地球半径
    	$dlng =  2*asin(sin($radius/(2*$er))/cos($curLatitude*M_PI/180));
    	$dlng = $dlng*180/M_PI;//角度转为弧度
        $dlat = $radius/$er;
        $dlat = $dlat*180/M_PI;        
        $minlat = $curLatitude-$dlat;
        $maxlat = $curLatitude+$dlat;
        $minlng = $curLongitude -$dlng;
        $maxlng = $curLongitude + $dlng;
        //查询边界内位置
    	$bus_infos = DB::select('select * from `bus_infos` where longitude >= :minlng and longitude <= :maxlng and latitude >= :minlat and latitude <= :maxlat and isDelete <> 1',['minlng'=>$minlng,'maxlng'=>$maxlng,'minlat'=>$minlat,'maxlat'=>$maxlat]);
    	
    	return json_encode($bus_infos);
    }

}
